<template>
    <div class="reportmian">
        <el-collapse v-model="activeNames">
            <el-collapse-item title="数据筛选" name="1">
                <el-form :inline="true" class="divstyle" :model="selectfrom">
                    <el-form-item label="部门">
                        <el-select filterable  placeholder="请选择部门" v-model="selectfrom.Department">
                            <el-option :value="0" label="全部"></el-option>
                            <el-option v-for="v in AllDepartment" :label="v.DepartmentName" :value="v.Id"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="项目">
                        <el-select  filterable placeholder="请选择项目" v-model="selectfrom.Project">
                            <el-option :value="0" label="全部"></el-option>
                            <el-option v-for="v in AllProject" :label="v.ProjectName" :value="v.Id"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item>
                        <el-date-picker
                                v-model="selectfrom.Start"
                                type="date"
                                placeholder="开始时间"
                                value-format="yyyy-MM-dd"
                                format="yyyy - MM - dd">
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item>
                        <el-date-picker
                                v-model="selectfrom.End"
                                type="date"
                                placeholder="结束时间"
                                value-format="yyyy-MM-dd"
                                format="yyyy - MM - dd">
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="success" icon="el-icon-search" @click="formdata"></el-button>
                        <el-button type="success" icon="el-icon-printer" v-print="printObj">打印</el-button>
                    </el-form-item>
                </el-form>
            </el-collapse-item>
            <el-collapse-item title="数据面板" name="2">
                <el-row>
                    <el-col align="center" style="font-size: 25px;font-weight: bold;">固远晨通科技发展有限公司入库单汇总表</el-col>
                    <el-col>
                        <el-table id="wrapperInboundorderReport" :data="tableData"  border  show-summary>
                            <el-table-column type="index" label="序号" width="50" align="center"></el-table-column>
                            <el-table-column label="入库单号" align="center" width="120" >
                                <template slot-scope="slot">
                                    {{ slot.row.InboundNumber }}
                                </template>
                            </el-table-column>
                            <el-table-column label="日期" align="center" width="120">
                                <template slot-scope="slot">
                                    {{ fmtDateNew(slot.row.OperatDate) }}
                                </template>
                            </el-table-column>
                            <el-table-column label="部门" align="center" width="120">
                                <template slot-scope="slot">
                                    {{ slot.row.DepartmentName  }}
                                </template>
                            </el-table-column>
                            <el-table-column label="项目名称" align="center" width="240">
                                <template slot-scope="slot">
                                    {{ slot.row.ProjectName }}
                                </template>
                            </el-table-column>
                            <el-table-column label="经办人" align="center" width="100">
                                <template slot-scope="slot">
                                    {{ slot.row.UserName }}
                                </template>
                            </el-table-column>
                            <el-table-column label="供应商信息" align="center" width="100">
                                <template slot-scope="slot">
                                    {{ GetCustomerName(slot.row.cuname) }}
                                </template>
                            </el-table-column>
                            <el-table-column label="物品名称" align="center" width="100">
                                <template slot-scope="slot">
                                    {{ slot.row.DeviceName }}
                                </template>
                            </el-table-column>
                            <el-table-column label="规格型号" align="center" width="100">
                                <template slot-scope="slot">
                                    {{ slot.row.DeviceModel  }}
                                </template>
                            </el-table-column>
                            <el-table-column label="单位" align="center" width="100">
                                <template slot-scope="slot">
                                    {{ slot.row.Unit }}
                                </template>
                            </el-table-column>
                            <el-table-column label="数量" align="center" width="100">
                                <template slot-scope="slot">
                                    {{ slot.row.Amount }}
                                </template>
                            </el-table-column>
                            <el-table-column label="单价" align="center" width="100">
                                <template slot-scope="slot">
                                    {{ slot.row.Price  }}
                                </template>
                            </el-table-column>
                            <el-table-column label="金额(元)" align="center" width="100" prop="TotalPrice"></el-table-column>
                            <el-table-column label="来源" align="center" width="100">
                                <template slot-scope="slot">
                                    {{ source(slot.row.PurchaseNumber) }}
                                </template>
                            </el-table-column>
                            <el-table-column label="备注" align="center">
                                <template slot-scope="slot">
                                    {{ slot.row.remark  }}
                                </template>
                            </el-table-column>
                        </el-table>
                    </el-col>
                </el-row>
            </el-collapse-item>
            <el-collapse-item title="数据报表" name="3">
                <div id="EchartData">
                    <div class="pie">
                        <div class="pieData" id="leftData"></div>
                        <div class="pieData" id="rightData"></div>
                    </div>
                    <div class="bar" id="downData"></div>
                </div>
            </el-collapse-item>
        </el-collapse>
    </div>
</template>

<script>
    import * as publicjs from "../../../../public/Scriput/Public";
    import Echatrs from "echarts";

    export default {
        name: "InboundorderReport",
        props:{
            AllProject:{
                type:Array,
                required:true
            },
            AllDepartment:{
                type:Array,
                required:true
            },
            AllCustomer:{
                type:Array,
                required:true
            }
        },
        data(){
            return{
                activeNames: ['1','2','3'],
                selectfrom:{
                    Start:new Date(),
                    End:new Date(),
                    Department:0,
                    Project:0,
                },
                tableData:[],
                wrapper: {
                    width: "100%",
                    height: "100%",
                    margin: "auto",
                },
                printObj: {
                    id: "wrapperInboundorderReport",  //打印标签的id
                    popTitle: '固远晨通科技发展有限公司入库单汇总表',  //文件标题
                    extraCss: 'http://www.guyuanct.com/',
                    extraHead: '<meta http-equiv="Content-Language"content="zh-cn"/>',
                },
            }
        },
        created() {

            this.InitDate();
            this.formdata()
            publicjs.setecharts(Echatrs)
        },
        methods: {
            InitDate() {
                const endDate = this.getFormatDate(new Date()).substr(0, 11)
                const beginDate = this.getFormatDate(new Date(new Date() - 3600 * 1000 * 24 * 30)).substr(0, 11)
                this.selectfrom.Start = beginDate;
                this.selectfrom.End = endDate;
            },
            // 获取当前时间
            getFormatDate(date) {
                var month = date.getMonth() + 1
                var strDate = date.getDate()
                if (month >= 1 && month <= 9) {
                    month = '0' + month
                }
                if (strDate >= 0 && strDate <= 9) {
                    strDate = '0' + strDate
                }
             var currentDate = date.getFullYear() + '-' + month + '-' + strDate + '';
                return currentDate
            },
            // 初始数据加载
            formdata() {
                let loadding = this.openLoading();
                this.$http.post('Report/GetDetialInboundorderReport',{
                    Start:this.selectfrom.Start,
                    End:this.selectfrom.End,
                    ProjectId:this.selectfrom.Project,
                    DepartmentId:this.selectfrom.Department,
                }).then(res => {
                    loadding.close();
                    res.data.forEach(d=>{
                        d.DepartmentName = this.GetDepartmentName(d.DepartmentId)
                        d.ProjectName  = this.GetProjectName(d.ProjectId)
                    })
                    this.tableData = res.data
                    this.InitEchart(this.tableData)
                })
            },
            //时间转化
            fmtDateNew(val){
                if (val !='' && val != null){
                    let newtime=val.split(' ')[0]
                    return newtime
                }else{
                    return ''
                }
            },
            //获取对应部门名字
            GetDepartmentName(value){
                let name=''
                if (value!=0){
                    for(let i=0;i<this.AllDepartment.length;i++){
                        if (this.AllDepartment[i].Id == value){
                            name = this.AllDepartment[i].DepartmentName
                            return name
                        }
                    }
                }
            },
            //获取对应项目名字
            GetProjectName(value){
                let name=''
                if (value!=0){
                    for(let i=0;i<this.AllProject.length;i++){
                        if (this.AllProject[i].Id == value){
                            name = this.AllProject[i].ProjectName
                            return name
                        }
                    }
                }
            },
            //获取人员名称
            GetUserName(value){
                let name=''
                if (value!=0){
                    for(let i=0;i<this.$store.state.AllUsers.length;i++){
                        if (this.$store.state.AllUsers[i].Id == value){
                            name = this.$store.state.AllUsers[i].UserName
                            return name
                        }
                    }
                }
            },
            //获取客户名称
            GetCustomerName(val){
                let name=''
                for (let i=0; i<this.AllCustomer.length; i++){
                    if (this.AllCustomer[i].Id == val){
                        name = this.AllCustomer[i].CustomerName
                        return name
                    }
                }
            },
            //金额转化
            conversion(val){
                if (val=='' || val == null){
                    return ''
                }else{
                    return val
                }
            },
            // echarts表格加载
            InitEchart(data){
                //按照部门分组统计
                let noRepeatDepartData = publicjs.DataGroupBy(JSON.stringify(data), "DepartmentName", "TotalPrice");
                let new_DepartName = noRepeatDepartData.map(obj => { return obj.DepartmentName; });
                let new_DepartData = noRepeatDepartData.map(obj => { return { name: obj.DepartmentName, value: obj.TotalPrice }; });
                publicjs.InitEchartPie("leftData", "部门金额占比", "单位：人民币（元）", new_DepartName, "部门金额占比", new_DepartData);
                //按照项目分组统计
                let noRepeatProjectData = publicjs.DataGroupBy(JSON.stringify(data), "ProjectId", "TotalPrice");
                let new_ProjectName = noRepeatProjectData.map(obj => { return obj.ProjectName == "" ? "其他" : obj.ProjectName; });
                let new_ProjectData = noRepeatProjectData.map(obj => { return { name: obj.ProjectName == "" ? "其他" : obj.ProjectName, value: obj.TotalPrice }; });
                publicjs.InitEchartPie("rightData", "项目金额占比", "单位：人民币（元）", new_ProjectName, "项目金额占比", new_ProjectData);
                //按照日期统计
                let noRepeatData = publicjs.DataGroupBy(JSON.stringify(data), "OperatDate", "TotalPrice");
                let new_Name = noRepeatData.map(obj => { return obj.OperatDate; });
                let new_Data = noRepeatData.map(obj => { return obj.TotalPrice; });
                publicjs.InitEchartBar("downData", "数据走势图", new_Name, new_Data, "金额（元）");
            },
            //物资来源
            source(val){
                let name=''
                if (val=='' || val==null){
                    name='物资库'
                }else{
                    name = "采购"
                }
                return name
            },
        }
    }
</script>

<style scoped>
    #EchartData .pieData {
        float: left;
        width: 50%;
        height: 500px;
    }
    #EchartData .bar {
        width: 100%;
        height: 500px;
    }
    #EchartData .pie {
        height: 500px;
    }
</style>
